package com.brsya.movie.presenter;

import com.brsya.base.base.BasePresenter;
import com.brsya.base.base.Contents;
import com.brsya.base.bean.BaseObjectBean;
import com.brsya.base.bean.DomainBean;
import com.brsya.base.bean.MovieListBean;
import com.brsya.base.net.MyObserver;
import com.brsya.base.net.RxScheduler;
import com.brsya.base.util.StringUtil;
import com.brsya.movie.contract.SplashContract;
import com.brsya.movie.model.SplashModel;

import java.util.Map;

/**
 * Created by Brsya
 * CreateDate: 2022/7/7  14:07
 * Description:
 * 启动页
 */
public class SplashPresenter extends BasePresenter<SplashContract.View> implements SplashContract.Presenter {
    private SplashContract.Model model;

    public SplashPresenter() {
        model = new SplashModel();
    }

    @Override
    public void getUrl() {
        if (mView == null) {
            return;
        }
        mView.showLoading();

        Map<String, Object> par = getPublicPar();

        model.getUrl(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseObjectBean<DomainBean>>(mView) {
                    @Override
                    public void success(BaseObjectBean<DomainBean> baseBean) {
                        if (mView == null) {
                            return;
                        }
                        baseBean.handleData(DomainBean.class);
                        if (baseBean.getData() == null || StringUtil.isEmpty(baseBean.getData().getDomainName())) {
                            // 获取失败
                            return;
                        }
                        Contents.BASE_URL = baseBean.getData().getDomainName();
                    }

                    @Override
                    public void complete(BaseObjectBean<DomainBean> baseBean) {
                        if (mView == null) {
                            return;
                        }
                        super.complete(baseBean);
                        mView.result();
                    }

                    @Override
                    public void error(Throwable e) {
                        if (mView == null) {
                            return;
                        }
                        super.error(e);
                        mView.result();
                    }
                });

    }
}
